﻿package net.wangit.adm.org;

import net.wangit.Unit;
import net.wangit.context.ActionContext;
import net.wangit.context.ActionContextHelper;
import net.wangit.context.LocalContext;
import net.wangit.support.UnitInterceptor;
import net.wangit.user.org.Org;
import net.wangit.user.org.OrgFactory;
import net.wangit.util.Checker;
import net.wangit.view.unit.UnitComponent;

public class OrgPrivilegeUI implements UnitInterceptor {

	private static final String DOMAIN = "net.wangit.adm.org.OrgPrivilegeUI.domain";
	private static final String ORGANIZATION_ID = "net.wangit.adm.org.OrgPrivilegeUI.orgid";

	public OrgPrivilegeUI() {
	}

	public String intercept(ActionContext ac, Unit unit) throws Exception {
		String oid = getAndSetOrganizationId();
		if (Checker.isEmpty(oid)) {
			return "";
		} else {
			unit.getElement("domain").setDefaultValue(getAndSetDomain());
			Org organization = OrgFactory.getOrg(oid);
			unit.setTitle((new StringBuilder(String
					.valueOf(((UnitComponent) unit).getTitle()))).append(" - ")
					.append(organization.getName()).append("(").append(oid)
					.append(")").toString());
			return unit.write(ac);
		}
	}

	public static String getAndSetDomain() {
		ActionContext ac = LocalContext.getLocalContext().getActionContext();
		return ActionContextHelper.getRequest2Session(ac,
				"org.privileges.domain",
				"net.wangit.adm.org.OrgPrivilegeUI.domain", "app");
	}

	public static String getAndSetOrganizationId() {
		ActionContext ac = LocalContext.getLocalContext().getActionContext();
		return ActionContextHelper.getRequest2Session(ac, "orgid",
				"net.wangit.adm.org.OrgPrivilegeUI.orgid", null);
	}
}
